//
//  ViewController.swift
//  ShowTableToSwift
//
//  Created by licc on 2020/6/8.
//  Copyright © 2020 licc. All rights reserved.
//

import Cocoa

class ViewController: NSViewController {

   
    @IBOutlet var textview0: NSTextView!
    
    @IBOutlet var textview1: NSTextView!
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override var representedObject: Any? {
        didSet {
        // Update the view, if already loaded.
        }
    }

    @IBAction func action0(_ sender: Any) {
        let generalPasteboard = NSPasteboard.general
        let value = generalPasteboard.string(forType: NSPasteboard.PasteboardType.init("NSStringPboardType"))
        print(value)
        if let vv = value  {
            if (vv.count == 0 || vv == self.textview1.string) {

            }else{

                self.textview0.string = vv
            }
        }
        
    }
    
   @IBAction func action1(_ sender: Any) {
    var cstr = "\">"
    var cstrarr = [String]()
    cstrarr.append("\" class=\"chooseItem active\">")
    cstrarr.append("\" class=\"colum-item chooseItem active\">")
//    cstrarr.append()
//    cstrarr.append()
//    cstrarr.append()
//    cstrarr.append()
//    cstrarr.append()
    cstrarr.append("\">")

    
    
    var outarr = GetHtmlPraArr(self.textview0.string, fromstr: "th data-field=\"", tostr: "</th>")
    if(outarr.count == 0){
         outarr = GetHtmlPraArr(self.textview0.string, fromstr: "<span column=\"", tostr: "</span>")
//         cstr = "\" class=\"chooseItem active\">"
    }
    if(outarr.count == 0){
         outarr = GetHtmlPraArr(self.textview0.string, fromstr: "<span column=\"", tostr: "</span>")
//         cstr = "\" class=\"colum-item chooseItem active\">"
    }
    print(outarr)
    var outstr = ""
    
    for str in outarr{
        var abarr =  str.components(separatedBy:cstr )
        for mstr in cstrarr{
            abarr =  str.components(separatedBy:mstr )
            if abarr.count == 2{
                break
            }
        }
        if abarr.count == 2{
            outstr = outstr + "    \t[\"\(abarr[1])\" :\(abarr[0])  ],\n"
        }

    }
    outstr = "[" + outstr + "]" +  "\n\n"
    for str in outarr{
        let abarr =  str.components(separatedBy:cstr )
        if abarr.count == 2{
            outstr = outstr + "    \tvar\t\(abarr[0])\t\t :String = \"\" \t\t//\(abarr[1])\n"
        }

    }
        textview1.string = outstr
    
    }
    
     @IBAction   func action2(_ sender: Any) {
        let b = NSPasteboard.general
        b.declareTypes([NSPasteboard.PasteboardType.init("NSStringPboardType")], owner: self)
        b.setString(textview1.string, forType: NSPasteboard.PasteboardType.init("NSStringPboardType"))
        
    }
    
     @IBAction   func action3(_ sender: Any) {
        action0("")
        action1("")
        action2("")

    }
//    <th data-checkbox="true"></th>
//    <th data-field="status">办理状态</th>
//    <th data-field="jobNumber">工单号</th>
//    <th data-field="eventTitle">事件标题</th>
//    <th data-field="createTime">创建时间</th>
//    <th data-field="infoSource">信息来源</th>
//    <th data-field="operator">录入人</th>
//    <th data-field="reportPerson">上报人</th>
//    <th data-field="phone">上报人联系方式</th>
//    <th data-field="occurrenceTime">发生时间</th>
        
        
        func GetHtmlPra(_ htmlstr:String, fromstr:String, tostr:String) -> String {
               if(htmlstr.count == 0){
                   return "";
               }
                     //1. 创建拼配规则
               let pattern3 = String.init(format: "(?<=%@)(.*)(?=%@)", fromstr,tostr)
                     //2. 创建正则拼配对象
                     let regex3 = try? NSRegularExpression(pattern: pattern3, options: [])
                     //3. 匹配字符串
                     guard let results3 = regex3?.matches(in: htmlstr, options: [], range:
                         NSRange(location: 0, length: htmlstr.count)) else
                     {
                         print("没有匹配结果")
                         return "";
                     }
                     //遍历匹配结果
                     for result3 in results3
                     {
                       return (htmlstr as NSString).substring(with: result3.range)
                     }
               return ""
               
               
           }
    func GetHtmlPraArr(_ htmlstr:String, fromstr:String, tostr:String) -> [String ]{
           if(htmlstr.count == 0){
               return [];
           }
                 //1. 创建拼配规则
           let pattern3 = String.init(format: "(?<=%@)(.*)(?=%@)", fromstr,tostr)
                 //2. 创建正则拼配对象
                 let regex3 = try? NSRegularExpression(pattern: pattern3, options: [])
                 //3. 匹配字符串
                 guard let results3 = regex3?.matches(in: htmlstr, options: [], range:
                     NSRange(location: 0, length: htmlstr.count)) else
                 {
                     print("没有匹配结果")
                     return [];
                 }
                 //遍历匹配结果
        var arr:[String] = [String]()
         for result3 in results3
         {
           let str =  (htmlstr as NSString).substring(with: result3.range)
            arr.append(str)
         }
        return arr
           
           
       }
}

